package com.server.rule.component.triple.complex;


import org.jdom.Element;

import com.server.rule.component.triple.Triple;
import com.server.rule.data.Data;

import com.server.rule.data.ExceptionTypeData;


public class AndTriple extends ComplexTriple{

	@Override
	public boolean match(Data data) throws ExceptionTypeData {
		for(Triple t:listeTriple){
			if(!t.match(data)) return false;
		}
		return true;
	}	
	
	
	public Element getXMLElement() {
		Element xmlElement = new Element("Triple");
		
		xmlElement.setAttribute("type","AndTriple");
		for(Triple t:listeTriple){
			xmlElement.addContent(t.getXMLElement());
		}
		return xmlElement;
	}
	
	//@Override
	public boolean equals(Triple p) {
		if(!(p instanceof AndTriple)) return false;
		if(listeTriple.size()!= ((AndTriple) p).listeTriple.size()) return false;
		
		for(Triple t1 : listeTriple){
			boolean match = false;
			for(Triple t2 : ((AndTriple) p).listeTriple ){
				if(t1.equals(t2)) match = true;
			}
			if(!match) return false;
		}
		return true;
	}

	@Override
	public String getHumanReadable() {
		String retour ="AND(";		
		for(Triple t:listeTriple){
			retour = retour+t.getHumanReadable()+",";
		}
		retour = retour.substring(0, retour.length()-1);
		retour = retour+")";
		return retour;
	}
	
}
